﻿using CQRS.Events;
using CQRS.Sample.Domain.BOs;
using CQRS.Sample.Domain.Commands;

namespace CQRS.Sample.Domain.CommandHandlers
{
    public class CreatePlaneCommandHandler : ICommandHandler<CreatePlaneCommand>
    {
        private readonly IDomainRepository<Event> _repository;

        public CreatePlaneCommandHandler(IDomainRepository<Event> repository)
        {
            _repository = repository;
        }

        #region ICommandHandler<CreatePlaneCommand> Members

        public void Execute(CreatePlaneCommand theEvent)
        {
            var pilot = new Pilot(theEvent.PilotName, theEvent.PilotSurame, theEvent.FlightsMadeByPilot);
            var plane = new Plane(theEvent.PlaneName, theEvent.Manufacturer, theEvent.YearManufactured,
                                  theEvent.FlightsMadeByPlane, pilot);

            _repository.Add(plane);
            _repository.Commit();
        }

        #endregion
    }
}